/*
 * NervusChainArray.cpp
 *
 *  Created on: Oct 25, 2017
 *      Author: david
 */

#include "NervusChainArray.h"

NervusChainArray::NervusChainArray(Complex defaultNervusVaule) {
	for( int i=0; i<NERVUS_CHAIN_COUNT; i++ )
		m_chain[i] = new JuliaNervusChain( NERVUS_COUNT, NERVUS_SIGNAL_COUNT, defaultNervusVaule );
	for( int i=0; i<NERVUS_CHAIN_COUNT; i++ )
	{
		int nIdx;
		nIdx=0;
		for( int j=0; j<NERVUS_CHAIN_COUNT; j++ )
		{
			if( i!=j )
				m_chain[i]->SetOtherChain( nIdx++, m_chain[j] );
		}
	}
}

NervusChainArray::~NervusChainArray() {
	for( int i=0; i<NERVUS_CHAIN_COUNT; i++ )
		delete m_chain[i];
}

void NervusChainArray::SetChainParam( int chainIdx, int nTargetIdx, JuliaChainDefine& cDef )
{
	m_chain[chainIdx]->SetChainParam( nTargetIdx, cDef );
}

bool NervusChainArray::RunCase( CTrainingCase* pCase )
{
	while( ProcessSignal( pCase->GetNextInputSignal() ))
	{};

	return pCase->CompareNervusOutputSignal( GetResult() ) == RES_GOOD;
}

bool NervusChainArray::ProcessSignal( DigitalSignal input )
{
	bool ret;
	for( int i=0; i<NERVUS_CHAIN_COUNT; i++ )
		ret = m_chain[i]->ProcessSignal( input.GetValue(i) );

	return ret;
}

DigitalSignal NervusChainArray::GetResult( void )
{
	for( int i=0; i<NERVUS_CHAIN_COUNT; i++ )
		m_Result.SetValue( i, m_chain[i]->GetResult() );
	return m_Result;
}
